<?php

namespace App\Models;

use App\Traits\ModelTrait;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class UserModel extends Authenticatable implements JWTSubject
{
    use HasFactory, Notifiable, ModelTrait;

    protected $table = 'users';

    // 指定日期字段
    protected $dates = ['create_time'];

    protected $casts = [
        'create_time' => 'datetime',
    ];

    const CREATED_AT = 'create_time';

    const UPDATED_AT = 'update_time';

    protected $fillable = ["uuid", "name", 'nickname', "email", 'password', 'vip_level','gender', 'birthday', 'age','avatar',
        'status','introduce','fans','last_login_time', 'last_active_time', 'deleted'];

    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * Get the identifier that will be stored in the subject claim of the token.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the token.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }

    public static function userCreate($params)
    {
        $params['tenant_id'] = session('tenantId');
        $params['app_package_id'] = session('appPackageId');
        return parent::create($params);
    }

    public static function updateLastLoginTime($userId)
    {
        $res = UserModel::whereId($userId)->update(['last_login_time' => Carbon::now()->toDateTimeString()]);
        return $res;
    }

    public static function updateLastActiveTime($userId)
    {
        $res = UserModel::whereId($userId)->update(['last_active_time' => Carbon::now()->toDateTimeString()]);
        return $res;
    }

    public static function getById($id)
    {
        return self::where('deleted', 0)->find($id);
    }

    public static function updateById($id, $data)
    {
        return self::whereId($id)->update($data);
    }
}
